package com.example.mybatisflexdemo.controller;

import com.example.mybatisflexdemo.convertor.UserConvertor;
import com.example.mybatisflexdemo.dto.UserAddDTO;
import com.example.mybatisflexdemo.dto.UserDTO;
import com.example.mybatisflexdemo.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisflexdemo.vo.UserAddVO;
import com.example.mybatisflexdemo.vo.UserVO;

import java.util.List;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 用于UserDTO/UserVO等之间的转换
     */
    private final UserConvertor userConvertor;

    @GetMapping
    List<UserVO> getUserList() {
        List<UserDTO> userList = userService.getUserList();
        return userConvertor.convertDtoToVoList(userList);
    }

    @PostMapping
    String addUser(UserAddVO user) {
        UserAddDTO userAddDTO = userConvertor.convertAddVoToAddDto(user);
        if (userService.addUser(userAddDTO) == 1) {
            return "success";
        }
        return "fail";
    }

    @DeleteMapping("/{id}")
    String deleteUser(@PathVariable Integer id) {
        if (userService.delUser(id) == 1) {
            return "success";
        }
        return "fail";
    }

}
